﻿namespace RefactoringMethods
{
    using System;
    
    internal class Student
    {
        private string firstName;
        private string lastName;
        private DateTime birthDate;

        internal Student(string firstName, string lastName, string birthDate)
        {
            DateTime parsedBirthDate = new DateTime();

            bool isDateParsed = DateTime.TryParse(birthDate, out parsedBirthDate);

            if (!isDateParsed)
            {
                throw new FormatException("Error! Invalid date format");
            }

            this.FirstName = firstName;
            this.LastName = lastName;
            this.BirthDate = parsedBirthDate;
        }

        internal string FirstName
        {
            get
            {
                return this.firstName;
            }

            private set
            {
                if (value.Length < 3)
                {
                    throw new ArgumentOutOfRangeException("Error! First name is too short");
                }

                this.firstName = value;
            }
        }

        internal string LastName
        {
            get
            {
                return this.lastName;
            }

            private set
            {
                if (value.Length < 3)
                {
                    throw new ArgumentOutOfRangeException("Error! Last name is too short");
                }

                this.lastName = value;
            }
        }

        internal DateTime BirthDate
        {
            get
            {
                return this.birthDate;
            }

            private set
            {
                if (value.Year > DateTime.Now.Year)
                {
                    throw new ArgumentException("Error! Invalid date year");
                }
                this.birthDate = value;
            }
        }

        internal bool IsOlderThan(Student studentToCompare)
        {
            bool isOlder = this.BirthDate > studentToCompare.BirthDate;

            return isOlder;
        }
    }
}